// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import org.joml.Vector2i;

public class DiamondMatrixIterator {
    private int size;
    private int lineSize;
    private int line;
    private int column;

    public DiamondMatrixIterator(int _size) {
        this.size = _size;
        this.lineSize = 1;
        this.line = 0;
        this.column = 0;
    }

    public DiamondMatrixIterator reset(int _size) {
        this.size = _size;
        this.lineSize = 1;
        this.line = 0;
        this.column = 0;
        return this;
    }

    public void reset() {
        this.lineSize = 1;
        this.line = 0;
        this.column = 0;
    }

    public boolean next(Vector2i vector2i) {
        if (this.lineSize == 0) {
            vector2i.x = 0;
            vector2i.y = 0;
            return false;
        } else if (this.line == 0 && this.column == 0) {
            vector2i.set(0, 0);
            this.column++;
            return true;
        } else {
            if (this.column < this.lineSize) {
                vector2i.x++;
                vector2i.y--;
                this.column++;
            } else {
                this.column = 1;
                this.line++;
                if (this.line < this.size) {
                    this.lineSize++;
                    vector2i.x = 0;
                    vector2i.y = this.line;
                } else {
                    this.lineSize--;
                    vector2i.x = this.line - this.size + 1;
                    vector2i.y = this.size - 1;
                }
            }

            if (this.lineSize == 0) {
                vector2i.x = 0;
                vector2i.y = 0;
                return false;
            } else {
                return true;
            }
        }
    }

    public Vector2i i2line(int int2) {
        int int0 = 0;

        for (int int1 = 1; int1 < this.size + 1; int1++) {
            int0 += int1;
            if (int2 + 1 <= int0) {
                return new Vector2i(int2 - int0 + int1, int1 - 1);
            }
        }

        for (int int3 = this.size + 1; int3 < this.size * 2; int3++) {
            int0 += this.size * 2 - int3;
            if (int2 + 1 <= int0) {
                return new Vector2i(int2 - int0 + this.size * 2 - int3, int3 - 1);
            }
        }

        return null;
    }

    public Vector2i line2coord(Vector2i vector2i0) {
        if (vector2i0 == null) {
            return null;
        } else if (vector2i0.y < this.size) {
            Vector2i vector2i1 = new Vector2i(0, vector2i0.y);

            for (int int0 = 0; int0 < vector2i0.x; int0++) {
                vector2i1.x++;
                vector2i1.y--;
            }

            return vector2i1;
        } else {
            Vector2i vector2i2 = new Vector2i(vector2i0.y - this.size + 1, this.size - 1);

            for (int int1 = 0; int1 < vector2i0.x; int1++) {
                vector2i2.x++;
                vector2i2.y--;
            }

            return vector2i2;
        }
    }
}
